package com.itheima.edu.info.manager.thread;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class TeacherSocketRunnable implements Runnable {
    private Socket socket;

    public TeacherSocketRunnable(Socket accept) {
        this.socket = accept;
    }
    TeacherService service = new TeacherService();

    @Override

    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String work = br.readLine();
            String[] split = work.split(",");
            switch (split[0]) {
                case "[1]":
                    addTeacher(split);
                    break;
                case "[2]":
                    deleteTeacher(split[1]);
                    break;
                case "[3]":
                   updateTeacher(split);
                    break;
                case "[4]":
                    System.out.println("[4]");
                    findAllTeacher();
                    break;
                case "[5]":
                    isExists(split[1]);
                    break;
                default:
                    System.out.println("输入有误!");
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void updateTeacher(String[] split) throws IOException {
        Teacher teacher=new Teacher(split[1],split[2],split[3],split[4]);
        service.updateTeacher(split[1],teacher);
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("修改成功");
        bw.flush();
        socket.shutdownOutput();
        socket.close();

    }

    private void deleteTeacher(String delId) throws IOException {
        service.deleteTeacher(delId);
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("删除成功");
        bw.flush();
        socket.shutdownOutput();
        socket.close();
    }

    private void isExists(String s) throws IOException {
        boolean exists = service.isExists(s);
        //将结果写会客户端

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(exists + "");
        bw.flush();
        socket.close();
    }

    private void addTeacher(String[] split) throws IOException {
        Teacher teacher = new Teacher(split[1], split[2], split[3], split[4]);
        boolean b = service.addTeacher(teacher);
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(b+"");
        bw.flush();
        socket.shutdownOutput();
        socket.close();

    }

    private void findAllTeacher() throws IOException {

        Teacher[] allTeacher = service.findAllTeacher();
        ArrayList<Teacher> list = new ArrayList<>(List.of(allTeacher));
        System.out.println(list);
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(list);
        socket.close();
    }
}
